package com.eobard.controller;

import com.eobard.config.obfuscate.ObfuscatorConfig;
import com.eobard.enums.PyInstallEnum;
import com.eobard.enums.RunningEnum;
import com.eobard.params.*;
import com.eobard.executor.ObfuscationExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;

/**
 * @Author: Eobard Gu
 * @Date: 2025-08-04
 * @Description: 测试接口
 */
@Controller
public class IndexController {
    @Autowired
    private ObfuscatorConfig obfuscatorConfig;

    @Autowired
    private ObfuscationExecutor executor;

    @Autowired
    private Executor threadPoolExecutor;

    @GetMapping("/rename")
    public ResponseEntity rename() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/IdeaProjects/MultiFramework/src/main/java");

        RenameClassParams renameClassParams = RenameClassParams.builder()
//                .isReferencedClassOnly(false)
                .packageList(List.of("com.eobard.controller"))
                .build();

        obfuscatorConfig.setRenameClassParams(renameClassParams);
//        executor.obfuscate(obfuscatorConfig);

        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("/md5")
    public ResponseEntity md5() {
        obfuscatorConfig.setResourceRoot("/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/res");
        AlterMD5Params md5Params = AlterMD5Params.builder()
                .audioSourceDirs(List.of("raw", "raw-ur", "raw-en"))
                .audioSaveTargetDirs(List.of("/Users/eobardgu/Desktop/混淆/t"))
                .imageSourceDirs(List.of("drawable-xhdpi"))
                .imageSaveTargetDirs(List.of("/Users/eobardgu/Desktop/混淆/t"))
                .build();
        obfuscatorConfig.setAlterMD5Params(md5Params);

        //异步混淆
//        threadPoolExecutor.execute(() -> executor.obfuscate(obfuscatorConfig));

        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("/package")
    public ResponseEntity package1() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/IdeaProjects/Sa-Token-Test/src/main/java");

        PackageRemoveParams packageRemoveParams = PackageRemoveParams.builder()
                .delOldFiles(false)
                .sourcePackages(List.of("com.eobard.util"))
                .targetPackages(List.of("com.eobard.utils"))
                .build();

        obfuscatorConfig.setPackageRemoveParams(packageRemoveParams);

        executor.obfuscate(obfuscatorConfig);
        return new ResponseEntity(getRes(), HttpStatus.OK);
    }

    @GetMapping("method")
    public ResponseEntity method() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/java");
        obfuscatorConfig.setRunningStatus(RunningEnum.ANDROID_PROjECT);
        //可变参数不能被混淆 (Object...args)
        //Unsolved symbol : We are unable to find the method declaration corresponding to LinkedListUtils.ywckpCJz(1)
        RenameMethodParams params = RenameMethodParams.builder()
                .isReferenceMethodOnly(false)
                .basePackage("com.fla.hdo")
                .packageList(List.of(
                        "com.fla.hdo.abc",
                        "com.fla.hdo.base",
                        "com.fla.hdo.beans",
                        "com.fla.hdo.call",
                        "com.fla.hdo.cfg",
                        "com.fla.hdo.def",
                        "com.fla.hdo.natives",
                        "com.fla.hdo.util"))
                .build();
        obfuscatorConfig.setRenameMethodParams(params);

        executor.obfuscate(obfuscatorConfig);
        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("argus")
    public ResponseEntity argus() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/java");
        obfuscatorConfig.setRunningStatus(RunningEnum.ANDROID_PROjECT);

        MethodArgumentParams params = MethodArgumentParams.builder()
                .isReferenceMethodOnly(false)
                .basePackage("com.fla.hdo")
                .packageList(List.of(
                        "com.fla.hdo.abc",
                        "com.fla.hdo.base",
                        "com.fla.hdo.beans",
                        "com.fla.hdo.call",
                        "com.fla.hdo.cfg",
                        "com.fla.hdo.def",
                        "com.fla.hdo.natives",
                        "com.fla.hdo.util"))
                .minAddParamNum(1)
                .maxAddParamNum(3)
                .build();

        obfuscatorConfig.setMethodArgumentParams(params);
        executor.obfuscate(obfuscatorConfig);
        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("over")
    public ResponseEntity over() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/java");
        obfuscatorConfig.setRunningStatus(RunningEnum.ANDROID_PROjECT);
        //会增加一个包
        OverrideMethodArgumentParams params = OverrideMethodArgumentParams.builder()
                .basePackage("com.fla.hdo")
                .minAddParamNum(1)
                .maxAddParamNum(3)
                .build();

        obfuscatorConfig.setOverrideMethodParams(params);

        executor.obfuscate(obfuscatorConfig);

        return new ResponseEntity(getRes(), HttpStatus.OK);
    }

    @GetMapping("resource")
    public ResponseEntity respource() {
        String sourceRoot = "/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/java";
        String resourceRoot = "/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/res";

        obfuscatorConfig.setSourceRoot(sourceRoot);
        obfuscatorConfig.setResourceRoot(resourceRoot);
        obfuscatorConfig.setRunningStatus(RunningEnum.ANDROID_PROjECT);

        RenameResourceParams params = RenameResourceParams.builder()
                .dirs(List.of("raw", "raw-en", "raw-ur", "drawable"))
                .resourceOldPrefix(List.of("com_pxt_living", "com_pask_living"))
                .resourceNewPrefix(List.of("com_gxp_living"))
                .build();

        obfuscatorConfig.setRenameResourceParams(params);

        executor.obfuscate(obfuscatorConfig);

        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("field")
    public ResponseEntity field() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/java");

        RenameFieldParams params = RenameFieldParams.builder()
                .basePackage("com.fla.hdo")
                .packageList(List.of(
                        "com.fla.hdo.abc",
                        "com.fla.hdo.base",
                        "com.fla.hdo.beans",
                        "com.fla.hdo.call",
                        "com.fla.hdo.cfg",
                        "com.fla.hdo.def",
                        "com.fla.hdo.natives",
                        "com.fla.hdo.util"))
                .build();

        obfuscatorConfig.setRenameFieldParams(params);

        executor.obfuscate(obfuscatorConfig);

        return new ResponseEntity(getRes(), HttpStatus.OK);
    }

    @GetMapping("two")
    public ResponseEntity two() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/java");
        obfuscatorConfig.setRunningStatus(RunningEnum.ANDROID_PROjECT);

        MethodArgumentParams params = MethodArgumentParams.builder()
                .isReferenceMethodOnly(false)
                .basePackage("com.fla.hdo")
                .packageList(List.of(
                        "com.fla.hdo.abc",
                        "com.fla.hdo.base",
                        "com.fla.hdo.beans",
                        "com.fla.hdo.call",
                        "com.fla.hdo.cfg",
                        "com.fla.hdo.def",
                        "com.fla.hdo.natives",
                        "com.fla.hdo.util"))
                .minAddParamNum(1)
                .maxAddParamNum(3)
                .build();

        obfuscatorConfig.setMethodArgumentParams(params);

        OverrideMethodArgumentParams params1 = OverrideMethodArgumentParams.builder()
                .basePackage("com.fla.hdo")
                .minAddParamNum(1)
                .maxAddParamNum(3)
                .build();

        obfuscatorConfig.setOverrideMethodParams(params1);
        executor.obfuscate(obfuscatorConfig);

        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("randomPkg")
    public ResponseEntity randomPkg() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/IdeaProjects/Sa-Token-Test/src/main/java");

        RandomRenamePackageParams params = RandomRenamePackageParams.builder()
                .basePackage("com.eobard")
//                .excludePackages(Set.of("com.eobard.ZZ2tjGuy","com.eobard.ZZIbxL9U"))
                .build();

        obfuscatorConfig.setRandomRenamePackageParams(params);

        executor.obfuscate(obfuscatorConfig);
        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("class")
    public ResponseEntity class1() {
        obfuscatorConfig.setSourceRoot("/Users/eobardgu/IdeaProjects/Sa-Token-Test/src/main/java");
        ClassRandomShuffleParams params = ClassRandomShuffleParams.builder()
                .basePackage("com.eobard")
                .excludePackages(Set.of("com.eobard.A", "com.eobard.C", "com.eobard.D"))
                .build();

        obfuscatorConfig.setClassRandomShuffleParams(params);
        executor.obfuscate(obfuscatorConfig);
        return new ResponseEntity(getRes(), HttpStatus.OK);
    }


    @GetMapping("mnn")
    public ResponseEntity mnn() {
        MnnConverterParams params = MnnConverterParams.builder()
                .mnnInstallRoot("/Users/eobardgu/mnn_workspace/MNN/")
                .installEnum(PyInstallEnum.PIP)
                .mnnModelPath("/Users/eobardgu/thai_project/action-live-mix-app-android/sdk/src/main/assets/models")
                .forwardConverter(false)
//                .converterPath("/Users/eobardgu/mnn_converter_output")
//                .mnnModelNames(List.of("Ik_v2.mnn", "Dt_v2.mnn"))
                .build();

        obfuscatorConfig.setMnnConverterParams(params);
        executor.obfuscate(obfuscatorConfig);
        return new ResponseEntity(getRes(), HttpStatus.OK);
    }

    private Map getRes() {
        Map<String, Object> res = new HashMap<>();
        res.put("source", obfuscatorConfig.getSourceRoot());
        res.put("resource", obfuscatorConfig.getResourceRoot());
        res.put("rename class", obfuscatorConfig.getRenameClassParams());
        res.put("alter md5", obfuscatorConfig.getAlterMD5Params());
        res.put("rename method", obfuscatorConfig.getRenameMethodParams());
        res.put("package remove", obfuscatorConfig.getPackageRemoveParams());
        res.put("method arguments", obfuscatorConfig.getMethodArgumentParams());
        res.put("override method", obfuscatorConfig.getOverrideMethodParams());
        res.put("running status", obfuscatorConfig.getRunningStatus());
        res.put("rename resource", obfuscatorConfig.getRenameResourceParams());
        res.put("rename field", obfuscatorConfig.getRenameFieldParams());
        res.put("random package", obfuscatorConfig.getRandomRenamePackageParams());
        res.put("class shuffle", obfuscatorConfig.getClassRandomShuffleParams());
        res.put("mnn", obfuscatorConfig.getMnnConverterParams());
        return res;
    }

}
